package com.xiaoying.server.publish.upload;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import com.xiaoying.server.publish.comomon.upyun.UpYunUtil;
import com.xiaoying.server.publish.entity.FileInfo;
import com.xiaoying.server.publish.utils.ObjectMessageConverter;
import com.xiaoying.server.publish.utils.RandomUtil;


public class ProduceFileInfo2MQ {

	private static int fileId=-1;
	private static String baseDir = System.getProperty("user.dir")+"\\file";
	
	public static void main(String[] args){		
    	ObjectMessageConverter objectMessageConverter = new ObjectMessageConverter();
    	ProduceFileInfo2MQ produceFileInfo = new ProduceFileInfo2MQ();
    	// 读取本地文件
    	List<File> files = produceFileInfo.readFiles();
    	// ConnectionFactory ：连接工厂，JMS 用它创建连接
        ConnectionFactory connectionFactory;
        Connection connection=null;
        // 构造ConnectionFactory实例对象，此处采用ActiveMq的实现jar
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");	// "tcp://localhost:61616?jms.useAsyncSend=true"
        
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置
            Destination destination = session.createQueue("xiaoying");
            // 得到消息生成者【发送者】 MessageProducer:消息发送者
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化,此处学习,实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 构造消息，此处写死，项目就是参数，或者方法获取
            for(int i=0;i<500;i++){            	
        		int fileIdCurrent = RandomUtil.getRandomSeedUnderN(files.size());
        		if(fileId==-1){
        			fileId = fileIdCurrent;
        		}else if(fileId==fileIdCurrent){
        			fileIdCurrent++;
        			fileIdCurrent = fileId%files.size();	//防止超过最大值
        		}            	
            	File file = files.get(fileIdCurrent);
            	short fileType  = 0;
            	if(file.getName().contains("video")){
            		fileType = UpYunUtil.FILETYPE_VIDEO;
            	}else{
            		fileType = UpYunUtil.FILETYPE_IMAGE_POSTER;
            	}
                producer.send(objectMessageConverter.toMessage(new FileInfo(file,i,fileType), session));
                session.commit();
                long sleepTime = RandomUtil.getRandomSleepTime();
                System.out.println("第"+i+"条发送成功! 用时"+sleepTime+"ms");
//                Thread.sleep(sleepTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Exception e2) {
               e2.printStackTrace();
            }
        }
        // 以下对象赋值null
        objectMessageConverter = null;
        produceFileInfo = null;
        files = null;
        connectionFactory = null;
        connection=null;
        connectionFactory = null;   
	}
	
    /**
     * 读取本地文件
     * @return
     */
	public List<File> readFiles(){
		List<File> files = new ArrayList<File>();		
		File file1 = new File(baseDir+"\\"+"video_corolla_test1.mp4");
		File file2 = new File(baseDir+"\\"+"video_corolla_test2.mp4");
		File file3 = new File(baseDir+"\\"+"video_corolla_test3.mp4");
		File file4 = new File(baseDir+"\\"+"video_corolla_test4.mp4");
		File file5 = new File(baseDir+"\\"+"video_corolla_test5.mp4");
		
		File file6 = new File(baseDir+"\\"+"image_corolla_test1.jpg");
		File file7 = new File(baseDir+"\\"+"image_corolla_test2.jpg");
		File file8 = new File(baseDir+"\\"+"image_corolla_test3.jpg");
		File file9 = new File(baseDir+"\\"+"image_corolla_test4.jpg");
		File file10 = new File(baseDir+"\\"+"image_corolla_test5.jpg");
		
		if(file1.isFile()){
			files.add(file1);
		}
		if(file2.isFile()){
			files.add(file2);
		}
		if(file3.isFile()){
			files.add(file3);
		}
		if(file4.isFile()){
			files.add(file4);
		}
		if(file5.isFile()){
			files.add(file5);
		}
		
		if(file6.isFile()){
			files.add(file6);
		}
		if(file7.isFile()){
			files.add(file7);
		}
		if(file8.isFile()){
			files.add(file8);
		}
		if(file9.isFile()){
			files.add(file9);
		}
		if(file10.isFile()){
			files.add(file10);
		}
		
		return files;
	}
    
	
	@Test
	public void testit() {
	    String dir = System.getProperty("user.dir")+"\\file";
	    String filePath = dir+"\\"+"video_corolla_test1.mp4";
	    File file1 = new File(filePath);
	    System.out.println(filePath); 
	    if(file1.exists()) {
	        System.out.println(file1.getName());
	    }
	}
	
	
	
}
